package cn.fresh.xuanyuan.framework.common.util.wx;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//import com.douyin.openapi.client.Client;
//import com.douyin.openapi.client.models.V2TokenRequest;
//import com.douyin.openapi.client.models.V2TokenResponse;
//import com.douyin.openapi.credential.models.Config;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class WxUtil {

    private final static Logger log = LoggerFactory.getLogger(WxUtil.class);

    private static final String UPLOAD_SHIPPING_INFO = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=";

    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    private static final String DOU_YIN_ACCESS_TOKEN_URL = "https://developer.toutiao.com/api/apps/v2/token";

    private static final String DOU_YIN_PUSH = "https://developer.toutiao.com/api/apps/order/v2/push";



    /**
     * 同步订单状态
     * @param accessToken
     * @param params
     * @return
     */
    public static String uploadShippingInfo(String accessToken, Map<String,Object> params) {
        String url = UPLOAD_SHIPPING_INFO + accessToken;
        String result = HttpUtil.post(url, JSON.toJSONString(params));
        return result;
    }

    public static String getAccessToken(String appId, String secret) {
        String url = ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + secret;
        String result = HttpUtil.get(url);
        if(StringUtils.isNotBlank(result)) {
            JSONObject json = JSON.parseObject(result);
            return json.getString("access_token");
        }
        return null;
    }

    public static String getDouYinToken(String appId, String secret) {
        try {
//            Config config = new Config().setClientKey(appId).setClientSecret(secret); // 改成自己的app_id跟secret
//            Client client = new Client(config);
//            V2TokenRequest sdkRequest = new V2TokenRequest();
//            sdkRequest.setAppid(appId);
//            sdkRequest.setGrantType("client_credential");
//            sdkRequest.setSecret(secret);
//            V2TokenResponse sdkResponse = client.V2Token(sdkRequest);
            Map<String, Object> map = new HashMap<>();
            map.put("appid",appId);
            map.put("secret",secret);
            map.put("grant_type","client_credential");
            String res = HttpUtil.post(DOU_YIN_ACCESS_TOKEN_URL, JSON.toJSONString(map));
            if(StringUtils.isNotBlank(res)) {
                JSONObject json = JSON.parseObject(res);
                if(json.getIntValue("err_no") == 0 && json.getJSONObject("data") != null) {
                    JSONObject data = json.getJSONObject("data");
                    return data.getString("access_token");
                }
            }
        }
        catch (Exception e) {
//            log.error("getDouYinToken exception:",e);e
        }
       return null;
    }

    public static String douYinPush(Map<String, Object> params) {
        String res = HttpUtil.post(DOU_YIN_PUSH, JSON.toJSONString(params));
        return res;
    }

}
